/*/{Protheus.doc} SYNCIDF
E a User Function de geracao do layout COD (Linhas de Nota Fiscal) do Sistema Synchro
@author Elton Teodoro Alves
@since 14/11/2014
@version P11 R5
@Param aParam,Array,Array enviado pelo schudele
@Example
Esta User Function foi desenvolvida para ser utilizada com o Schudele, que passa como parametro para User Function um array com a seguinte configuracao:</br>
</br>
<table>
<tr><th> POSICAO </th><th> TIPO </th><th> DESCRICAO             </th></tr>
<tr><td> [01]    </td><td> C    </td><td> Codigo da Empresa     </td></tr>
<tr><td> [02]    </td><td> C    </td><td> Codigo da Filial      </td></tr>
<tr><td> [03]    </td><td> C    </td><td> Codigo do Agendamento </td></tr>
<tr><td> [04]    </td><td> C    </td><td> Codigo da Tarefa      </td></tr>
</table>
</br>	
Para teste fora do schudele, pode-se enviar para o SmartClient com a clausula -a estes dados separadas por virgula ',' Ex.: 99,01,0001,0001
/*/

#Define CRLF Chr(13) + Chr(10)
Static PERIOD := 'D'

#Include 'Protheus.ch'

User Function SYNCIDF(xParam)
	
	Local	cLayout		:=	Right(ProcName(),3)
	Local	aParam		:=	If(Valtype(xParam) == 'C',Separa(xParam,',',.T.),xParam)
	Local	aSyncEnv	:=	U_SYNCENV(aParam,cLayout,PERIOD)
	Local	cTrab		:=	GetNextAlias()
	Local	cTempTrab	:=	cLayout + Dtos(Date()) + StrTran(Time(),':','')
	Local	cTempFile	:=	GetSrvProfString("StartPath","") + cTempTrab + GetDbExtension()
	Local	cQuerySD1	:=	''
	Local	cQuerySD2	:=	''
	Local	cQuery		:=	''
	Local	cQueryBkp	:=	''
	Local	aStru		:=	{}
	Local	nX			:=	0
	Local	aFiliais	:=	StrTokArr(AllTrim(aSyncEnv[10]),'/',)
	Local	cFiliais	:=	''
	Local	nCount		:=	0
	Local	aUf 		:=	{}
	
	// Monta Array com codgios da UFs
	
	aadd(aUF,{"RO","11"})
	aadd(aUF,{"AC","12"})
	aadd(aUF,{"AM","13"})
	aadd(aUF,{"RR","14"})
	aadd(aUF,{"PA","15"})
	aadd(aUF,{"AP","16"})
	aadd(aUF,{"TO","17"})
	aadd(aUF,{"MA","21"})
	aadd(aUF,{"PI","22"})
	aadd(aUF,{"CE","23"})
	aadd(aUF,{"RN","24"})
	aadd(aUF,{"PB","25"})
	aadd(aUF,{"PE","26"})
	aadd(aUF,{"AL","27"})
	aadd(aUF,{"MG","31"})
	aadd(aUF,{"ES","32"})
	aadd(aUF,{"RJ","33"})
	aadd(aUF,{"SP","35"})
	aadd(aUF,{"PR","41"})
	aadd(aUF,{"SC","42"})
	aadd(aUF,{"RS","43"})
	aadd(aUF,{"MS","50"})
	aadd(aUF,{"MT","51"})
	aadd(aUF,{"GO","52"})
	aadd(aUF,{"DF","53"})
	aadd(aUF,{"SE","28"})
	aadd(aUF,{"BA","29"})
	aadd(aUF,{"EX","  "})
	aadd(aUF,{"  ","  "})
	
	// Monta a string com a lista de filiais para ser utilizada na query
	
	For nX := 1 To Len(aFiliais)
		
		cFiliais += "'" + aFiliais[nX] + "'"
		
		If nX # Len(aFiliais)
			
			cFiliais += ","
			
		End If
		
	Next nX
	
	// --------------------------------------------------------------
	// Monta estrutura do arquivo temporario com dados a serem exportados
	// --------------------------------------------------------------
	
	aAdd(aStru,{'POS0001','C',0007,0}) // ALIQ_ICMS
	aAdd(aStru,{'POS0008','C',0007,0}) // ALIQ_II
	aAdd(aStru,{'POS0015','C',0007,0}) // ALIQ_INSS
	aAdd(aStru,{'POS0022','C',0007,0}) // ALIQ_IPI
	aAdd(aStru,{'POS0029','C',0007,0}) // ALIQ_ISS
	aAdd(aStru,{'POS0036','C',0007,0}) // ALIQ_STF
	aAdd(aStru,{'POS0043','C',0005,0}) // AM_CODIGO
	aAdd(aStru,{'POS0048','C',0007,0}) // CFOP_CODIGO
	aAdd(aStru,{'POS0055','C',0001,0}) // CTRL_INSTRUCAO
	aAdd(aStru,{'POS0056','C',0040,0}) // DOF_IMPORT_NUMERO
	aAdd(aStru,{'POS0096','C',0008,0}) // DT_DI
	aAdd(aStru,{'POS0104','C',0003,0}) // ENTSAI_UNI_CODIGO
	aAdd(aStru,{'POS0107','C',0003,0}) // ESTOQUE_UNI_CODIGO
	aAdd(aStru,{'POS0110','C',0007,0}) // FAT_BASE_CALCULO_STF
	aAdd(aStru,{'POS0117','C',0010,0}) // FAT_CONV_UNI_ESTOQUE
	aAdd(aStru,{'POS0127','C',0005,0}) // FIN_CODIGO
	aAdd(aStru,{'POS0132','C',0004,0}) // IDF_NUM
	aAdd(aStru,{'POS0136','C',0050,0}) // IDF_TEXTO_COMPLEMENTAR
	aAdd(aStru,{'POS0186','C',0018,0}) // INFORMANTE_EST_CODIGO
	aAdd(aStru,{'POS0204','C',0020,0}) // MERC_CODIGO
	aAdd(aStru,{'POS0224','C',0005,0}) // MUN_CODIGO
	aAdd(aStru,{'POS0229','C',0010,0}) // NBM_CODIGO
	aAdd(aStru,{'POS0239','C',0007,0}) // NOP_CODIGO
	aAdd(aStru,{'POS0246','C',0010,0}) // NUM_DI
	aAdd(aStru,{'POS0256','C',0001,0}) // OM_CODIGO
	aAdd(aStru,{'POS0257','C',0012,0}) // PESO_BRUTO_KG
	aAdd(aStru,{'POS0269','C',0012,0}) // PESO_LIQUIDO_KG
	aAdd(aStru,{'POS0281','C',0021,0}) // PRECO_TOTAL
	aAdd(aStru,{'POS0302','C',0021,0}) // PRECO_UNITARIO
	aAdd(aStru,{'POS0323','C',0003,0}) // PRES_CODIGO
	aAdd(aStru,{'POS0326','C',0012,0}) // QTD
	aAdd(aStru,{'POS0338','C',0004,0}) // LIVRE
	aAdd(aStru,{'POS0342','C',0002,0}) // STC_CODIGO
	aAdd(aStru,{'POS0344','C',0001,0}) // STP_CODIGO
	aAdd(aStru,{'POS0345','C',0001,0}) // SUBCLASSE_IDF
	aAdd(aStru,{'POS0346','C',0003,0}) // UNI_FISCAL_CODIGO
	aAdd(aStru,{'POS0349','C',0021,0}) // VL_BASE_ICMS
	aAdd(aStru,{'POS0370','C',0021,0}) // VL_BASE_IPI
	aAdd(aStru,{'POS0391','C',0021,0}) // VL_BASE_IRRF
	aAdd(aStru,{'POS0412','C',0021,0}) // VL_BASE_ISS
	aAdd(aStru,{'POS0433','C',0021,0}) // VL_BASE_STF
	aAdd(aStru,{'POS0454','C',0021,0}) // VL_CONTABIL
	aAdd(aStru,{'POS0475','C',0021,0}) // VL_FATURADO
	aAdd(aStru,{'POS0496','C',0021,0}) // VL_FISCAL
	aAdd(aStru,{'POS0517','C',0021,0}) // VL_ICMS
	aAdd(aStru,{'POS0538','C',0021,0}) // VL_II
	aAdd(aStru,{'POS0559','C',0021,0}) // VL_INSS
	aAdd(aStru,{'POS0580','C',0021,0}) // VL_IPI
	aAdd(aStru,{'POS0601','C',0021,0}) // VL_IRRF
	aAdd(aStru,{'POS0622','C',0021,0}) // VL_ISENTO_ICMS
	aAdd(aStru,{'POS0643','C',0021,0}) // VL_ISENTO_IPI
	aAdd(aStru,{'POS0664','C',0021,0}) // VL_ISS
	aAdd(aStru,{'POS0685','C',0021,0}) // VL_OUTROS_ICMS
	aAdd(aStru,{'POS0706','C',0021,0}) // VL_OUTROS_IPI
	aAdd(aStru,{'POS0727','C',0021,0}) // VL_STF
	aAdd(aStru,{'POS0748','C',0021,0}) // VL_TRIBUTAVEL_ICMS
	aAdd(aStru,{'POS0769','C',0021,0}) // VL_TRIBUTAVEL_IPI
	aAdd(aStru,{'POS0790','C',0021,0}) // VL_TRIBUTAVEL_STF
	aAdd(aStru,{'POS0811','C',0003,0}) // PHI_LG
	aAdd(aStru,{'POS0814','C',0005,0}) // PHI_BG
	aAdd(aStru,{'POS0819','C',0004,0}) // PHI_BU
	aAdd(aStru,{'POS0823','C',0010,0}) // SISS_CODIGO
	aAdd(aStru,{'POS0833','C',0005,0}) // CODIGO_RETENCAO
	aAdd(aStru,{'POS0838','C',0021,0}) // VL_ALIQ_COFINS
	aAdd(aStru,{'POS0859','C',0021,0}) // VL_ALIQ_PIS
	aAdd(aStru,{'POS0880','C',0021,0}) // VL_BASE_PIS
	aAdd(aStru,{'POS0901','C',0021,0}) // VL_BASE_COFINS
	aAdd(aStru,{'POS0922','C',0021,0}) // VL_IMPOSTO_PIS
	aAdd(aStru,{'POS0943','C',0021,0}) // VL_IMPOSTO_COFINS
	aAdd(aStru,{'POS0964','C',0017,0}) // PERC_TRIBUTAVEL_ISS
	aAdd(aStru,{'POS0981','C',0017,0}) // PERC_TRIBUTAVEL_IRRF
	aAdd(aStru,{'POS0998','C',0017,0}) // PERC_TRIBUTAVEL_INSS
	aAdd(aStru,{'POS1015','C',0017,0}) // PERC_TRIBUTAVEL_COFINS
	aAdd(aStru,{'POS1032','C',0017,0}) // PERC_TRIBUTAVEL_PIS
	aAdd(aStru,{'POS1049','C',0001,0}) // IND_PIS_ST
	aAdd(aStru,{'POS1050','C',0001,0}) // IND_COFINS_ST
	aAdd(aStru,{'POS1051','C',0012,0}) // CONTA_CONTABIL
	aAdd(aStru,{'POS1063','C',0021,0}) // VL_BASE_PIS_RET
	aAdd(aStru,{'POS1084','C',0017,0}) // ALIQ_PIS_RET
	aAdd(aStru,{'POS1101','C',0021,0}) // VL_PIS_RET
	aAdd(aStru,{'POS1122','C',0021,0}) // VL_BASE_COFINS_RET
	aAdd(aStru,{'POS1143','C',0017,0}) // ALIQ_COFINS_RET
	aAdd(aStru,{'POS1160','C',0021,0}) // VL_COFINS_RET
	aAdd(aStru,{'POS1181','C',0021,0}) // VL_BASE_CSLL_RET
	aAdd(aStru,{'POS1202','C',0017,0}) // ALIQ_CSLL_RET
	aAdd(aStru,{'POS1219','C',0021,0}) // VL_CSLL_RET
	aAdd(aStru,{'POS1240','C',0001,0}) // IND_MOV_FISICA (86)
	aAdd(aStru,{'POS1241','C',0017,0}) // ALIQ_IRRF
	aAdd(aStru,{'POS1258','C',0007,0}) // MUN_CODIGO
	aAdd(aStru,{'POS1265','C',0004,0}) // COD_CLASS
	aAdd(aStru,{'POS1269','C',0001,0}) // IND_REC
	aAdd(aStru,{'POS1270','C',0021,0}) // VL_DESC
	aAdd(aStru,{'POS1291','C',0002,0}) // CST_PIS
	aAdd(aStru,{'POS1293','C',0002,0}) // CST_COFINS
	aAdd(aStru,{'POS1295','C',0002,0}) // STP_CODIGO
	aAdd(aStru,{'POS1297','C',0021,0}) // VL_BASE_INSS
	aAdd(aStru,{'POS1318','C',0007,0}) // ALIQ_INSS_RET
	aAdd(aStru,{'POS1325','C',0021,0}) // VL_BASE_INSS_RET
	aAdd(aStru,{'POS1346','C',0021,0}) // VL_INSS_RET
	aAdd(aStru,{'POS1367','C',0021,0}) // VL_AJUSTE_PRECO_TOTAL
	aAdd(aStru,{'POS1388','C',0002,0}) // COD_BC_CREDITO
	aAdd(aStru,{'POS1390','C',0028,0}) // COD_CCUS
	aAdd(aStru,{'POS1418','C',0017,0}) // PERC_PARC_CI
	aAdd(aStru,{'POS1435','C',0036,0}) // FCI_NUMERO
	
	// --------------------------------------------------------------
	
	If aSyncEnv[6]
		
		// --------------------------------------------------------------
		// Montagem da query de pesquisa
		// --------------------------------------------------------------
		cQuerySD1 += "SELECT " + CRLF
		cQuerySD2 += "SELECT " + CRLF
		
		cQuerySD1 += "'SD1' ORIGEM," + CRLF
		cQuerySD2 += "'SD2' ORIGEM," + CRLF
		
		cQuerySD1 += "SD1.D1_FILIAL FILIAL," + CRLF
		cQuerySD2 += "SD2.D2_FILIAL FILIAL," + CRLF
		
		cQuerySD1 += "SF1.F1_EST UF," + CRLF
		cQuerySD2 += "SF2.F2_EST UF," + CRLF
		
		cQuerySD1 += "SF1.F1_DOC DOC," + CRLF
		cQuerySD2 += "SF2.F2_DOC  DOC," + CRLF
		
		cQuerySD1 += "SF1.F1_SERIE SERIE," + CRLF
		cQuerySD2 += "SF2.F2_SERIE SERIE," + CRLF
		
		cQuerySD1 += "SF1.F1_EMISSAO EMISSAO," + CRLF
		cQuerySD2 += "SF2.F2_EMISSAO EMISSAO," + CRLF
		
		cQuerySD1 += "SF1.R_E_C_N_O_ DOF_RECNO," + CRLF
		cQuerySD2 += "SF2.R_E_C_N_O_ DOF_RECNO," + CRLF
		
		cQuerySD1 += "SF4.F4_LFICM LFICM," + CRLF
		cQuerySD2 += "SF4.F4_LFICM LFICM," + CRLF
		
		cQuerySD1 += "SF4.F4_LFIPI LFIPI," + CRLF
		cQuerySD2 += "SF4.F4_LFIPI LFIPI," + CRLF
		
		cQuerySD1 += "SF1.F1_CHVNFE CHVNFE," + CRLF
		cQuerySD2 += "SF2.F2_CHVNFE CHVNFE," + CRLF
		
		cQuerySD1 += "SD1.D_E_L_E_T_ DELET," + CRLF
		cQuerySD2 += "SD2.D_E_L_E_T_ DELET," + CRLF
		
		cQuerySD1 += "SA2.A2_EST UF," + CRLF
		cQuerySD2 += "'' UF," + CRLF
		
		//---------------------------------------------------------------
		
		cQuerySD1 += "SD1.D1_PICM POS0001," + CRLF
		cQuerySD2 += "SD2.D2_PICM POS0001," + CRLF
		
		cQuerySD1 += "SD1.D1_ALIQII POS0008," + CRLF
		cQuerySD2 += "0 POS0008," + CRLF
		
		cQuerySD1 += "SD1.D1_ALIQINS POS0015," + CRLF
		cQuerySD2 += "SD2.D2_ALIQINS POS0015," + CRLF
		
		cQuerySD1 += "SD1.D1_IPI POS0022," + CRLF
		cQuerySD2 += "SD2.D2_IPI POS0022," + CRLF
		
		cQuerySD1 += "SD1.D1_ALIQISS POS0029," + CRLF
		cQuerySD2 += "SD2.D2_ALIQISS POS0029," + CRLF
		
		cQuerySD1 += "SD1.D1_ALIQSOL POS0036," + CRLF
		cQuerySD2 += "SD2.D2_ALIQSOL POS0036," + CRLF
		
		cQuerySD1 += "ZB3.ZB3_CDENTR POS0043," + CRLF
		cQuerySD2 += "ZB3.ZB3_CDSAID POS0043," + CRLF
		
		cQuerySD1 += "SD1.D1_CF POS0048," + CRLF
		cQuerySD2 += "SD2.D2_CF POS0048," + CRLF
		
		cQuerySD1 += "'I' POS0055," + CRLF
		cQuerySD2 += "'I' POS0055," + CRLF
		
		cQuerySD1 += "'' POS0056," + CRLF
		cQuerySD2 += "'' POS0056," + CRLF
		
		cQuerySD1 += "CD5.CD5_DTDI POS0096," + CRLF
		cQuerySD2 += "'' POS0096," + CRLF
		
		cQuerySD1 += "SAH.AH_XUMSYNC POS0104," + CRLF
		cQuerySD2 += "SAH.AH_XUMSYNC POS0104," + CRLF
		
		cQuerySD1 += "SAH.AH_XUMSYNC POS0107," + CRLF
		cQuerySD2 += "SAH.AH_XUMSYNC POS0107," + CRLF
		
		cQuerySD1 += "'+000,00' POS0110," + CRLF
		cQuerySD2 += "'+000,00' POS0110," + CRLF
		
		cQuerySD1 += "'+0001,0000' POS0117," + CRLF
		cQuerySD2 += "'+0001,0000' POS0117," + CRLF
		
		cQuerySD1 += "ZB3.ZB3_CDENTR POS0127," + CRLF
		cQuerySD2 += "ZB3.ZB3_CDSAID POS0127," + CRLF
		
		cQuerySD1 += "SD1.D1_ITEM POS0132," + CRLF
		cQuerySD2 += "SD2.D2_ITEM POS0132," + CRLF
		
		cQuerySD1 += "'' POS0136," + CRLF
		cQuerySD2 += "'' POS0136," + CRLF
		
		cQuerySD1 += "'' POS0186," + CRLF
		cQuerySD2 += "'' POS0186," + CRLF
		
		cQuerySD1 += "SD1.D1_COD POS0204," + CRLF
		cQuerySD2 += "SD2.D2_COD POS0204," + CRLF
		
		cQuerySD1 += "'' POS0224," + CRLF
		cQuerySD2 += "'' POS0224," + CRLF
		
		cQuerySD1 += "SB1.B1_POSIPI POS0229," + CRLF
		cQuerySD2 += "SB1.B1_POSIPI POS0229," + CRLF
		
		cQuerySD1 += "SF4.F4_XNOP POS0239," + CRLF
		cQuerySD2 += "SF4.F4_XNOP POS0239," + CRLF
		
		cQuerySD1 += "CD5.CD5_NDI POS0246," + CRLF
		cQuerySD2 += "'' POS0246," + CRLF
		
		cQuerySD1 += "SB1.B1_ORIGEM POS0256," + CRLF
		cQuerySD2 += "SB1.B1_ORIGEM POS0256," + CRLF
		
		cQuerySD1 += "SB1.B1_PESBRU POS0257," + CRLF
		cQuerySD2 += "SB1.B1_PESBRU POS0257," + CRLF
		
		cQuerySD1 += "SB1.B1_PESO POS0269," + CRLF
		cQuerySD2 += "SB1.B1_PESO POS0269," + CRLF
		
		cQuerySD1 += "SD1.D1_TOTAL POS0281," + CRLF
		cQuerySD2 += "SD2.D2_TOTAL POS0281," + CRLF
		
		cQuerySD1 += "SD1.D1_VUNIT POS0302," + CRLF
		cQuerySD2 += "SD2.D2_PRCVEN POS0302," + CRLF
		
		cQuerySD1 += "ZB3.ZB3_TPPRSV POS0323," + CRLF
		cQuerySD2 += "ZB3.ZB3_TPPRSV POS0323," + CRLF
		
		cQuerySD1 += "SD1.D1_QUANT POS0326," + CRLF
		cQuerySD2 += "SD2.D2_QUANT POS0326," + CRLF
		
		cQuerySD1 += "'' POS0338," + CRLF
		cQuerySD2 += "'' POS0338," + CRLF
		
		cQuerySD1 += "SF4.F4_SITTRIB POS0342," + CRLF
		cQuerySD2 += "SF4.F4_SITTRIB POS0342," + CRLF
		
		cQuerySD1 += "'' POS0344," + CRLF
		cQuerySD2 += "'' POS0344," + CRLF
		
		cQuerySD1 += "ZB6.ZB6_TPVLR POS0345," + CRLF
		cQuerySD2 += "ZB6.ZB6_TPVLR POS0345," + CRLF
		
		cQuerySD1 += "SAH.AH_XUMSYNC POS0346," + CRLF
		cQuerySD2 += "SAH.AH_XUMSYNC POS0346," + CRLF
		
		cQuerySD1 += "SD1.D1_BASEICM POS0349," + CRLF
		cQuerySD2 += "SD2.D2_BASEICM POS0349," + CRLF
		
		cQuerySD1 += "SD1.D1_BASEIPI POS0370," + CRLF
		cQuerySD2 += "SD2.D2_BASEIPI POS0370," + CRLF
		
		cQuerySD1 += "SD1.D1_BASEIRR POS0391," + CRLF
		cQuerySD2 += "SD2.D2_BASEIRR POS0391," + CRLF
		
		cQuerySD1 += "SD1.D1_BASEISS POS0412," + CRLF
		cQuerySD2 += "SD2.D2_BASEISS POS0412," + CRLF
		
		cQuerySD1 += "SD1.D1_BRICMS POS0433," + CRLF
		cQuerySD2 += "SD2.D2_BRICMS POS0433," + CRLF
		
		cQuerySD1 += "SD1.D1_TOTAL + SD1.D1_ICMSRET + SD1.D1_VALIPI POS0454," + CRLF
		cQuerySD2 += "SD2.D2_TOTAL + SD2.D2_ICMSRET + SD2.D2_VALIPI POS0454," + CRLF
		
		cQuerySD1 += "SD1.D1_TOTAL + SD1.D1_ICMSRET + SD1.D1_VALIPI POS0475," + CRLF
		cQuerySD2 += "SD2.D2_TOTAL + SD2.D2_ICMSRET + SD2.D2_VALIPI POS0475," + CRLF
		
		cQuerySD1 += "SD1.D1_TOTAL POS0496," + CRLF
		cQuerySD2 += "SD2.D2_TOTAL POS0496," + CRLF
		
		cQuerySD1 += "SD1.D1_VALICM POS0517," + CRLF
		cQuerySD2 += "SD2.D2_VALICM POS0517," + CRLF
		
		cQuerySD1 += "SD1.D1_II POS0538," + CRLF
		cQuerySD2 += "0 POS0538," + CRLF
		
		cQuerySD1 += "0 POS0559," + CRLF
		cQuerySD2 += "0 POS0559," + CRLF
		
		cQuerySD1 += "SD1.D1_VALIPI POS0580," + CRLF
		cQuerySD2 += "SD2.D2_VALIPI POS0580," + CRLF
		
		cQuerySD1 += "SD1.D1_VALIRR POS0601," + CRLF
		cQuerySD2 += "SD2.D2_VALIRRF POS0601," + CRLF
		
		cQuerySD1 += "SD1.D1_TOTAL - SD1.D1_BASEICM POS0622," + CRLF
		cQuerySD2 += "SD2.D2_TOTAL - SD2.D2_BASEICM POS0622," + CRLF
		
		cQuerySD1 += "SD1.D1_TOTAL - SD1.D1_BASEIPI POS0643," + CRLF
		cQuerySD2 += "SD2.D2_TOTAL - SD2.D2_BASEIPI POS0643," + CRLF
		
		cQuerySD1 += "SD1.D1_VALISS POS0664," + CRLF
		cQuerySD2 += "SD2.D2_VALISS POS0664," + CRLF
		
		cQuerySD1 += "'' POS0685," + CRLF
		cQuerySD2 += "'' POS0685," + CRLF
		
		cQuerySD1 += "SD1.D1_BASEIPI POS0706," + CRLF
		cQuerySD2 += "SD2.D2_BASEIPI POS0706," + CRLF
		
		cQuerySD1 += "SD1.D1_ICMSRET POS0727," + CRLF
		cQuerySD2 += "SD2.D2_ICMSRET POS0727," + CRLF
		
		cQuerySD1 += "SD1.D1_BASEICM POS0748," + CRLF
		cQuerySD2 += "SD2.D2_BASEICM POS0748," + CRLF
		
		cQuerySD1 += "SD1.D1_BASEIPI POS0769," + CRLF
		cQuerySD2 += "SD2.D2_BASEIPI POS0769," + CRLF
		
		cQuerySD1 += "SD1.D1_BRICMS POS0790," + CRLF
		cQuerySD2 += "SD2.D2_BRICMS POS0790," + CRLF
		
		cQuerySD1 += "'' POS0811," + CRLF
		cQuerySD2 += "'' POS0811," + CRLF
		
		cQuerySD1 += "'' POS0814," + CRLF
		cQuerySD2 += "'' POS0814," + CRLF
		
		cQuerySD1 += "'' POS0819," + CRLF
		cQuerySD2 += "'' POS0819," + CRLF
		
		cQuerySD1 += "SB1.B1_TRIBMUN POS0823," + CRLF
		cQuerySD2 += "SB1.B1_TRIBMUN POS0823," + CRLF
		
		cQuerySD1 += "SE2.E2_CODRET POS0833," + CRLF
		cQuerySD2 += "'' POS0833," + CRLF
		
		cQuerySD1 += "SD1.D1_ALQIMP5 POS0838," + CRLF
		cQuerySD2 += "SD2.D2_ALQIMP5 POS0838," + CRLF
		
		cQuerySD1 += "SD1.D1_ALQIMP6 POS0859," + CRLF
		cQuerySD2 += "SD2.D2_ALQIMP6 POS0859," + CRLF
		
		cQuerySD1 += "SD1.D1_BASIMP6 POS0880," + CRLF
		cQuerySD2 += "SD2.D2_BASIMP6 POS0880," + CRLF
		
		cQuerySD1 += "SD1.D1_BASIMP5 POS0901," + CRLF
		cQuerySD2 += "SD2.D2_BASIMP5 POS0901," + CRLF
		
		cQuerySD1 += "SD1.D1_VALIMP6 POS0922," + CRLF
		cQuerySD2 += "SD2.D2_VALIMP6 POS0922," + CRLF
		
		cQuerySD1 += "SD1.D1_VALIMP5 POS0943," + CRLF
		cQuerySD2 += "SD2.D2_VALIMP5 POS0943," + CRLF
		
		cQuerySD1 += "'' POS0964," + CRLF
		cQuerySD2 += "'' POS0964," + CRLF
		
		cQuerySD1 += "'' POS0981," + CRLF
		cQuerySD2 += "'' POS0981," + CRLF
		
		cQuerySD1 += "'' POS0998," + CRLF
		cQuerySD2 += "'' POS0998," + CRLF
		
		cQuerySD1 += "'' POS1015," + CRLF
		cQuerySD2 += "'' POS1015," + CRLF
		
		cQuerySD1 += "'' POS1032," + CRLF
		cQuerySD2 += "'' POS1032," + CRLF
		
		cQuerySD1 += "'N' POS1049," + CRLF
		cQuerySD2 += "'N' POS1049," + CRLF
		
		cQuerySD1 += "'N' POS1050," + CRLF
		cQuerySD2 += "'N' POS1050," + CRLF
		
		cQuerySD1 += "SB1.B1_CONTA POS1051," + CRLF
		cQuerySD2 += "SB1.B1_CONTA POS1051," + CRLF
		
		cQuerySD1 += "SD1.D1_BASEPIS POS1063," + CRLF
		cQuerySD2 += "SD2.D2_BASEPIS POS1063," + CRLF
		
		cQuerySD1 += "SD1.D1_ALQPIS POS1084," + CRLF
		cQuerySD2 += "SD2.D2_ALQPIS POS1084," + CRLF
		
		cQuerySD1 += "SD1.D1_VALPIS POS1101," + CRLF
		cQuerySD2 += "SD2.D2_VALPIS POS1101," + CRLF
		
		cQuerySD1 += "SD1.D1_BASECOF POS1122," + CRLF
		cQuerySD2 += "SD2.D2_BASECOF POS1122," + CRLF
		
		cQuerySD1 += "SD1.D1_ALQCOF POS1143," + CRLF
		cQuerySD2 += "SD2.D2_ALQCOF POS1143," + CRLF
		
		cQuerySD1 += "SD1.D1_VALCOF POS1160," + CRLF
		cQuerySD2 += "SD2.D2_VALCOF POS1160," + CRLF
		
		cQuerySD1 += "SD1.D1_BASECSL POS1181," + CRLF
		cQuerySD2 += "SD2.D2_BASECSL POS1181," + CRLF
		
		cQuerySD1 += "SD1.D1_ALQCSL POS1202," + CRLF
		cQuerySD2 += "SD2.D2_ALQCSL POS1202," + CRLF
		
		cQuerySD1 += "SD1.D1_VALCSL POS1219," + CRLF
		cQuerySD2 += "SD2.D2_VALCSL POS1219," + CRLF
		
		cQuerySD1 += "SF4.F4_ESTOQUE POS1240," + CRLF
		cQuerySD2 += "SF4.F4_ESTOQUE POS1240," + CRLF
		
		cQuerySD1 += "SD1.D1_ALIQIRR POS1241," + CRLF
		cQuerySD2 += "SD2.D2_ALQIRRF POS1241," + CRLF
		
		// CODIGO MUNICIPIO EMISSOR NF DE ENTRADA
		cQuerySD1 += "CASE SF1.F1_TIPO" + CRLF
		cQuerySD1 += "WHEN 'N' THEN SA2.A2_COD_MUN" + CRLF
		cQuerySD1 += "WHEN 'C' THEN SA2.A2_COD_MUN" + CRLF
		cQuerySD1 += "WHEN 'I' THEN SA2.A2_COD_MUN" + CRLF
		cQuerySD1 += "WHEN 'P' THEN SA2.A2_COD_MUN" + CRLF
		cQuerySD1 += "WHEN 'D' THEN SA1.A1_COD_MUN" + CRLF
		cQuerySD1 += "WHEN 'B' THEN SA1.A1_COD_MUN" + CRLF
		cQuerySD1 += "ELSE ''" + CRLF
		cQuerySD1 += "END POS1258," + CRLF
		// CODIGO MUNICIPIO EMISSOR NF DE SAIDA SERA O QUE CONSTA NO SIGAMAT
		cQuerySD2 += "'' POS1258," + CRLF
		
		cQuerySD1 += "'0000' POS1265," + CRLF
		cQuerySD2 += "'0000' POS1265," + CRLF
		
		cQuerySD1 += "SF4.F4_XTPREC POS1269," + CRLF
		cQuerySD2 += "SF4.F4_XTPREC POS1269," + CRLF
		
		cQuerySD1 += "'' POS1270," + CRLF
		cQuerySD2 += "'' POS1270," + CRLF
		
		cQuerySD1 += "SF4.F4_CSTPIS POS1291," + CRLF
		cQuerySD2 += "SF4.F4_CSTPIS POS1291," + CRLF
		
		cQuerySD1 += "SF4.F4_CSTCOF POS1293," + CRLF
		cQuerySD2 += "SF4.F4_CSTCOF POS1293," + CRLF
		
		cQuerySD1 += "SF4.F4_CTIPI POS1295," + CRLF
		cQuerySD2 += "SF4.F4_CTIPI POS1295," + CRLF
		
		cQuerySD1 += "'' POS1297," + CRLF
		cQuerySD2 += "'' POS1297," + CRLF
		
		cQuerySD1 += "SD1.D1_ALIQINS POS1318," + CRLF
		cQuerySD2 += "SD2.D2_ALIQINS POS1318," + CRLF
		
		cQuerySD1 += "SD1.D1_BASEINS POS1325," + CRLF
		cQuerySD2 += "SD2.D2_BASEINS POS1325," + CRLF
		
		cQuerySD1 += "SD1.D1_VALINS POS1346," + CRLF
		cQuerySD2 += "SD2.D2_VALINS POS1346," + CRLF
		
		cQuerySD1 += "'' POS1367," + CRLF
		cQuerySD2 += "'' POS1367," + CRLF
		
		cQuerySD1 += "SF4.F4_CODBCC POS1388," + CRLF
		cQuerySD2 += "SF4.F4_CODBCC POS1388," + CRLF
		
		cQuerySD1 += "SF4.F4_XCCSYNC POS1390," + CRLF
		cQuerySD2 += "SF4.F4_XCCSYNC POS1390," + CRLF
		
		cQuerySD1 += "SD1.D1_CONIMP POS1418," + CRLF
		cQuerySD2 += "0 POS1418," + CRLF
		
		cQuerySD1 += "'' POS1435" + CRLF //"SD1.D1_FCICOD POS1435" + CRLF
		cQuerySD2 += "SD2.D2_FCICOD POS1435" + CRLF
		
		
		//---- From SD1 SD2
		
		cQuerySD1	+=	"FROM " + RetSqlName("SD1") + " SD1 " + CRLF
		cQuerySD2	+=	"FROM " + RetSqlName("SD2") + " SD2 " + CRLF
		
		//---- Left Join SD1
		
		cQuerySD1	+=	"LEFT JOIN " + RetSqlName("SF1") + " SF1 ON" + CRLF
		cQuerySD1	+=	"SD1.D1_FILIAL = SF1.F1_FILIAL AND" + CRLF
		cQuerySD1	+=	"SD1.D1_SERIE = SF1.F1_SERIE AND" + CRLF
		cQuerySD1	+=	"SD1.D1_DOC = SF1.F1_DOC AND" + CRLF
		cQuerySD1	+=	"SD1.D1_EMISSAO = SF1.F1_EMISSAO AND" + CRLF
		cQuerySD1	+=	"SD1.D1_FORNECE = SF1.F1_FORNECE AND" + CRLF
		cQuerySD1	+=	"SD1.D1_LOJA = SF1.F1_LOJA" + CRLF
		
		cQuerySD1	+=	"LEFT JOIN " + RetSqlName("ZB3") + " ZB3 ON" + CRLF
		cQuerySD1	+=	"ZB3.ZB3_TIPO = SD1.D1_TP AND " + CRLF
		cQuerySD1	+=	"SD1.D1_FILIAL = ZB3.ZB3_FILIAL" + CRLF
		
		cQuerySD1	+=	"LEFT JOIN " + RetSqlName("ZB6") + " ZB6 ON" + CRLF
		cQuerySD1	+=	"ZB6.ZB6_TPDOCT = SF1.F1_ESPECIE " + CRLF
		
		cQuerySD1	+=	"LEFT JOIN " + RetSqlName("SF3") + " SF3 ON" + CRLF
		cQuerySD1	+=	"SF1.F1_FILIAL = SF3.F3_FILIAL AND" + CRLF
		cQuerySD1	+=	"SF1.F1_EMISSAO = SF3.F3_EMISSAO AND" + CRLF
		cQuerySD1	+=	"SF1.F1_DOC = SF3.F3_NFISCAL AND" + CRLF
		cQuerySD1	+=	"SF1.F1_SERIE = SF3.F3_SERIE AND" + CRLF
		cQuerySD1	+=	"SF1.F1_FORNECE = SF3.F3_CLIEFOR AND" + CRLF
		cQuerySD1	+=	"SF1.F1_LOJA = SF3.F3_LOJA " + CRLF
		
		cQuerySD1	+=	"LEFT JOIN " + RetSqlName("CD5") + " CD5 ON " + CRLF
		cQuerySD1	+=	"SF3.F3_FILIAL = CD5.CD5_FILIAL AND" + CRLF
		cQuerySD1	+=	"SF3.F3_NFISCAL = CD5.CD5_DOC AND" + CRLF
		cQuerySD1	+=	"SF3.F3_SERIE = CD5.CD5_SERIE AND" + CRLF
		cQuerySD1	+=	"SF3.F3_CLIEFOR = CD5.CD5_FORNEC AND" + CRLF
		cQuerySD1	+=	"SF3.F3_LOJA = CD5.CD5_LOJA " + CRLF
		
		cQuerySD1	+=	"LEFT JOIN " + RetSqlName("SAH") + " SAH ON " + CRLF
		cQuerySD1	+=	"SD1.D1_UM = SAH.AH_UNIMED " + CRLF
		
		cQuerySD1	+=	"LEFT JOIN " + RetSqlName("SB1") + " SB1 ON " + CRLF
		cQuerySD1	+=	"SD1.D1_FILIAL = SB1.B1_FILIAL AND " + CRLF
		cQuerySD1	+=	"SD1.D1_COD = SB1.B1_COD " + CRLF
		
		cQuerySD1	+=	"LEFT JOIN " + RetSqlName("SF4") + " SF4 ON " + CRLF
		cQuerySD1	+=	"SD1.D1_FILIAL = SF4.F4_FILIAL AND " + CRLF
		cQuerySD1	+=	"SD1.D1_TES = SF4.F4_CODIGO " + CRLF
		
		cQuerySD1	+=	"LEFT JOIN " + RetSqlName("SE2") + " SE2 ON " + CRLF
		cQuerySD1	+=	"SE2.E2_PREFIXO = SF1.F1_SERIE AND " + CRLF
		cQuerySD1	+=	"SE2.E2_NUM = SF1.F1_DOC AND " + CRLF
		cQuerySD1	+=	"SE2.E2_EMISSAO = SF1.F1_EMISSAO AND " + CRLF
		cQuerySD1	+=	"SE2.E2_FORNECE = SF1.F1_FORNECE AND " + CRLF
		cQuerySD1	+=	"SE2.E2_LOJA = SF1.F1_LOJA AND " + CRLF
		cQuerySD1	+=	"SE2.E2_PARCELA IN(' ','1') " + CRLF
		
		cQuerySD1	+=	"LEFT JOIN " + RetSqlName("SA1") + " SA1 " + CRLF
		cQuerySD1	+=	"ON SD1.D1_FORNECE = SA1.A1_COD AND " + CRLF
		cQuerySD1	+=	"SD1.D1_LOJA = SA1.A1_LOJA " + CRLF
		
		cQuerySD1	+=	"LEFT JOIN " + RetSqlName("SA2") + " SA2 " + CRLF
		cQuerySD1	+=	"ON SD1.D1_FORNECE = SA2.A2_COD AND " + CRLF
		cQuerySD1	+=	"SD1.D1_LOJA = SA2.A2_LOJA " + CRLF
		
		//---- Where SD1
		
		cQuerySD1	+=	"WHERE (SF1.F1_DTDIGIT = '" + aSyncEnv[9] + "' OR SF1.F1_DAUTNFE = '" + aSyncEnv[9] + "') AND" + CRLF
		cQuerySD1	+=	"SF1.F1_FILIAL IN(" + cFiliais + ") AND " + CRLF
		cQuerySD1	+=	"ISNULL(ZB3.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySD1	+=	"ISNULL(ZB6.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySD1	+=	"ISNULL(SF3.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySD1	+=	"ISNULL(CD5.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySD1	+=	"ISNULL(SAH.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySD1	+=	"ISNULL(SB1.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySD1	+=	"ISNULL(SF4.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySD1	+=	"ISNULL(SE2.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySD1	+=	"ISNULL(SA1.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySD1	+=	"ISNULL(SA2.D_E_L_E_T_,'') <> '*' " + CRLF
		
		//---- Left Join SD2
		
		cQuerySD2	+=	"LEFT JOIN " + RetSqlName("SF2") + " SF2 ON" + CRLF
		cQuerySD2	+=	"SD2.D2_FILIAL = SF2.F2_FILIAL AND" + CRLF
		cQuerySD2	+=	"SD2.D2_SERIE = SF2.F2_SERIE AND" + CRLF
		cQuerySD2	+=	"SD2.D2_DOC = SF2.F2_DOC AND" + CRLF
		cQuerySD2	+=	"SD2.D2_EMISSAO = SF2.F2_EMISSAO AND" + CRLF
		cQuerySD2	+=	"SD2.D2_CLIENTE = SF2.F2_CLIENTE AND" + CRLF
		cQuerySD2	+=	"SD2.D2_LOJA = SD2.D2_LOJA" + CRLF
		
		cQuerySD2	+=	"LEFT JOIN " + RetSqlName("ZB3") + " ZB3 ON" + CRLF
		cQuerySD2	+=	"ZB3.ZB3_TIPO = SD2.D2_TP AND " + CRLF
		cQuerySD2	+=	"SD2.D2_FILIAL = ZB3.ZB3_FILIAL" + CRLF
		
		cQuerySD2	+=	"LEFT JOIN " + RetSqlName("ZB6") + " ZB6 ON" + CRLF
		cQuerySD2	+=	"ZB6.ZB6_TPDOCT = SF2.F2_SERIE " + CRLF
		
		cQuerySD2	+=	"LEFT JOIN " + RetSqlName("SAH") + " SAH ON " + CRLF
		cQuerySD2	+=	"SD2.D2_UM = SAH.AH_UNIMED " + CRLF
		
		cQuerySD2	+=	"LEFT JOIN " + RetSqlName("SB1") + " SB1 ON " + CRLF
		cQuerySD2	+=	"SD2.D2_FILIAL = SB1.B1_FILIAL AND " + CRLF
		cQuerySD2	+=	"SD2.D2_COD = SB1.B1_COD " + CRLF
		
		cQuerySD2	+=	"LEFT JOIN " + RetSqlName("SF4") + " SF4 ON " + CRLF
		cQuerySD2	+=	"SD2.D2_FILIAL = SF4.F4_FILIAL AND " + CRLF
		cQuerySD2	+=	"SD2.D2_TES = SF4.F4_CODIGO " + CRLF
		
		//---- Where SD2
		
		cQuerySD2	+=	"WHERE SF2.F2_DAUTNFE = '" + aSyncEnv[9] + "' AND " + CRLF
		cQuerySD2	+=	"SF2.F2_FILIAL IN(" + cFiliais + ") AND " + CRLF
		cQuerySD2	+=	"SF2.F2_CHVNFE <> '" + Space(GETSX3CACHE('F2_CHVNFE','X3_TAMANHO')) + "' AND " + CRLF
		cQuerySD2	+=	"ISNULL(ZB3.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySD2	+=	"ISNULL(ZB6.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySD2	+=	"ISNULL(SAH.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySD2	+=	"ISNULL(SB1.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySD2	+=	"ISNULL(SF4.D_E_L_E_T_,'') <> '*'" + CRLF
		
		// --------------------------------------------------------------
		
		cQuery += cQuerySD1 + CRLF
		cQuery += "UNION ALL" + CRLF
		cQuery += cQuerySD2 + CRLF
		
		cQueryBkp	:=	cQuery
		
		cQuery := ChangeQuery(cQuery)
		
		If TcSqlExec(cQuery) < 0
			
			U_SYNCMAIL(AllTrim(aSyncEnv[15]),cLayout + ' - ' + AllTrim(aSyncEnv[17]),'Erro na execucao da query no banco de dados: ' + Chr(13) + Chr(10) + AllTrim(TcSqlError()),AllTrim(SubStr(aSyncEnv[4],1,6)),AllTrim(SubStr(aSyncEnv[4],7,6)),1,aSyncEnv[8],aSyncEnv[9])
			
		Else
			
			DbUseArea(.T.,"TOPCONN",TCGenQry(,,cQuery),cTrab,.F.,.T.)
			
			U_SYNCLOG(;
				'Agenda : ' +  SubStr(aSyncEnv[4],1,6) + CRLF +;
				'Tarefa : ' +  SubStr(aSyncEnv[4],7,6) + CRLF +;
				cLayout + ' - ' + AllTrim(aSyncEnv[17]) + CRLF +;
				'Query executada com sucesso: ' + CRLF + cQueryBkp)
			
			// Cria arquivo temporario
			
			fErase(Separa(cTempFile,'\',.T.)[3])
			DbCreate(cTempFile,aStru,__LocalDriver)
			DbUseArea(.T.,__LocalDriver,cTempFile,cTempTrab,.T.,.F.)
			
			U_SYNCLOG(;
				'Agenda : ' +  SubStr(aSyncEnv[4],1,6) + CRLF +;
				'Tarefa : ' +  SubStr(aSyncEnv[4],7,6) + CRLF +;
				'Arquivo Temporario criado com sucesso : ' + cTempFile)
			
			// --------------------------------------------------------------
			// Preenchimento do arquivo temporario
			// --------------------------------------------------------------
			
			(cTrab)->(DbGotop())
			
			Do While .Not. (cTrab)->(EOF())
				
				If (.Not. Empty((cTrab)->CHVNFE) .And. (cTrab)->DELET == '*') .Or. ((cTrab)->DELET <> '*')
					
					RecLock(cTempTrab,.T.)
					
					(cTempTrab)->POS0001 := '+' + StrTran(Transform((cTrab)->POS0001,'@E '+ Replicate('9',3) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0008 := '+' + StrTran(Transform((cTrab)->POS0008,'@E '+ Replicate('9',3) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0015 := '+' + StrTran(Transform((cTrab)->POS0015,'@E '+ Replicate('9',3) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0022 := '+' + StrTran(Transform((cTrab)->POS0022,'@E '+ Replicate('9',3) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0029 := '+' + StrTran(Transform((cTrab)->POS0029,'@E '+ Replicate('9',3) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0036 := '+' + StrTran(Transform((cTrab)->POS0036,'@E '+ Replicate('9',3) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0043 := (cTrab)->POS0043
					
					//--- Trata o CFOP quando se tratar de nota fiscal de servico
					
					If (cTrab)->POS0344 != 'S'
						
						(cTempTrab)->POS0048 := (cTrab)->POS0048
						
					Else
						
						If (cTrab)->ORIGEM == 'SF1'
							
							If (cTrab)->UF == U_SYNCFIL(aSyncEnv[7],6,(cTrab)->FILIAL)
								
								(cTempTrab)->POS0048 := '1000'
								
							ElseIf (cTrab)->UF != U_SYNCFIL(aSyncEnv[7],6,(cTrab)->FILIAL) .AND. (cTrab)->UF != 'EX'
								
								(cTempTrab)->POS0048 := '2000'
								
							ElseIf (cTrab)->UF == 'EX'
								
								(cTempTrab)->POS0048 := '3000'
								
							EndIf
							
						ElseIf (cTrab)->ORIGEM == 'SF2'
							
							If (cTrab)->UF == U_SYNCFIL(aSyncEnv[7],6,(cTrab)->FILIAL)
								
								(cTempTrab)->POS0048 := '5000'
								
							ElseIf (cTrab)->UF != U_SYNCFIL(aSyncEnv[7],6,(cTrab)->FILIAL) .AND. (cTrab)->UF != 'EX'
								
								(cTempTrab)->POS0048 := '6000'
								
							ElseIf (cTrab)->UF == 'EX'
								
								(cTempTrab)->POS0048 := '7000'
								
							EndIf
							
						End If
						
					End If
					
					//------------------------------------------------------
					
					(cTempTrab)->POS0055 := (cTrab)->POS0055
					(cTempTrab)->POS0056 := U_SYNCFIL(aSyncEnv[7],5,(cTrab)->FILIAL) + Str((cTrab)->DOF_RECNO,10) + (cTrab)->DOC + (cTrab)->SERIE + (cTrab)->EMISSAO
					(cTempTrab)->POS0096 := (cTrab)->POS0096
					(cTempTrab)->POS0104 := (cTrab)->POS0104
					(cTempTrab)->POS0107 := (cTrab)->POS0107
					(cTempTrab)->POS0110 := (cTrab)->POS0110
					(cTempTrab)->POS0117 := (cTrab)->POS0117
					(cTempTrab)->POS0127 := (cTrab)->POS0127
					(cTempTrab)->POS0132 := PadL(AllTrim(Str(Val((cTrab)->POS0132))),4,'0')
					(cTempTrab)->POS0136 := (cTrab)->POS0136
					(cTempTrab)->POS0186 := U_SYNCFIL(aSyncEnv[7],2,(cTrab)->FILIAL) + U_SYNCFIL(aSyncEnv[7],3,(cTrab)->FILIAL)
					(cTempTrab)->POS0204 := U_SYNCFIL(aSyncEnv[7],4,(cTrab)->FILIAL) + (cTrab)->POS0204
					(cTempTrab)->POS0224 := (cTrab)->POS0224
					(cTempTrab)->POS0229 := (cTrab)->POS0229
					(cTempTrab)->POS0239 := (cTrab)->POS0239
					(cTempTrab)->POS0246 := (cTrab)->POS0246
					(cTempTrab)->POS0256 := (cTrab)->POS0256
					(cTempTrab)->POS0257 := '+' + StrTran(Transform((cTrab)->POS0257,'@E '+ Replicate('9',7) + '.' + Replicate('9',3)),' ','0')
					(cTempTrab)->POS0269 := '+' + StrTran(Transform((cTrab)->POS0269,'@E '+ Replicate('9',7) + '.' + Replicate('9',3)),' ','0')
					(cTempTrab)->POS0281 := '+' + StrTran(Transform((cTrab)->POS0281,'@E '+ Replicate('9',15) + '.' + Replicate('9',4)),' ','0')
					(cTempTrab)->POS0302 := '+' + StrTran(Transform((cTrab)->POS0302,'@E '+ Replicate('9',15) + '.' + Replicate('9',4)),' ','0')
					(cTempTrab)->POS0323 := (cTrab)->POS0323
					(cTempTrab)->POS0326 := '+' + StrTran(Transform((cTrab)->POS0326,'@E '+ Replicate('9',7) + '.' + Replicate('9',3)),' ','0')
					(cTempTrab)->POS0338 := (cTrab)->POS0338
					(cTempTrab)->POS0342 := (cTrab)->POS0342
					(cTempTrab)->POS0344 := (cTrab)->POS0344
					(cTempTrab)->POS0345 := (cTrab)->POS0345
					(cTempTrab)->POS0346 := (cTrab)->POS0346
					(cTempTrab)->POS0349 := '+' + StrTran(Transform((cTrab)->POS0349,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0370 := '+' + StrTran(Transform((cTrab)->POS0370,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0391 := '+' + StrTran(Transform((cTrab)->POS0391,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0412 := '+' + StrTran(Transform((cTrab)->POS0412,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0433 := '+' + StrTran(Transform((cTrab)->POS0433,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0454 := '+' + StrTran(Transform((cTrab)->POS0454,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0475 := '+' + StrTran(Transform((cTrab)->POS0475,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0496 := '+' + StrTran(Transform((cTrab)->POS0496,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0517 := '+' + StrTran(Transform((cTrab)->POS0517,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0538 := '+' + StrTran(Transform((cTrab)->POS0538,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0559 := '+' + StrTran(Transform((cTrab)->POS0559,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0580 := '+' + StrTran(Transform((cTrab)->POS0580,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0601 := '+' + StrTran(Transform((cTrab)->POS0601,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					
					
					// Base ICMS Isenta
					If (cTrab)->LFICM == 'T'
						
						(cTempTrab)->POS0622 := '+' + StrTran(Transform((cTrab)->POS0622,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
						
					ElseIf (cTrab)->LFICM == 'I'
						
						(cTempTrab)->POS0622 := '+' + StrTran(Transform((cTrab)->POS0281,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
						
					Else
						
						(cTempTrab)->POS0622 := '+' + Replicate('0',17) + ',' + Replicate('0',2)
						
					End If
					
					// Base IPI Isenta
					If (cTrab)->LFIPI == 'T'
						
						(cTempTrab)->POS0643 := '+' + StrTran(Transform((cTrab)->POS0643,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
						
					ElseIf (cTrab)->LFIPI == 'I'
						
						(cTempTrab)->POS0643 := '+' + StrTran(Transform((cTrab)->POS0281,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
						
					Else
						
						(cTempTrab)->POS0643 := '+' + Replicate('0',17) + ',' + Replicate('0',2)
						
					End If
					
					//Valor do ISS
					(cTempTrab)->POS0664 := '+' + StrTran(Transform((cTrab)->POS0664,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					
					
					// Base ICMS Outras
					If (cTrab)->LFICM == 'O' .And. (cTrab)->LFIPI != 'P'
						
						(cTempTrab)->POS0685 := '+' + StrTran(Transform((cTrab)->POS0281,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
						
					ElseIf (cTrab)->LFICM == 'O' .And. (cTrab)->LFIPI == 'P'
						
						(cTempTrab)->POS0685 := '+' + StrTran(Transform((cTrab)->POS0281 + (cTrab)->POS0,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
						
					ElseIf (cTrab)->LFICM != 'O' .And. (cTrab)->LFIPI == 'P'
						
						(cTempTrab)->POS0685 := '+' + StrTran(Transform((cTrab)->POS0580,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
						
					Else
						
						(cTempTrab)->POS0685 := '+' + Replicate('0',17) + ',' + Replicate('0',2)
						
					End If
					
					// Base IPI Outras
					If (cTrab)->LFIPI == 'O'
						
						(cTempTrab)->POS0706 := '+' + StrTran(Transform((cTrab)->POS0706,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
						
					Else
						
						(cTempTrab)->POS0706 := '+' + Replicate('0',17) + ',' + Replicate('0',2)
						
					End If
					
					// Valor do ICMS-ST
					(cTempTrab)->POS0727 := '+' + StrTran(Transform((cTrab)->POS0727,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					
					// Base ICMS Tributavel
					If (cTrab)->LFICM == 'T'
						
						(cTempTrab)->POS0748 := '+' + StrTran(Transform((cTrab)->POS0748,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
						
					Else
						
						(cTempTrab)->POS0748 := '+' + Replicate('0',17) + ',' + Replicate('0',2)
						
					End IF
					
					// Base IPI Tributavel
					If (cTrab)->LFIPI == 'T'
						
						(cTempTrab)->POS0769 := '+' + StrTran(Transform((cTrab)->POS0769,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
						
					Else
						
						(cTempTrab)->POS0769 := '+' + Replicate('0',17) + ',' + Replicate('0',2)
						
					End If
					
					(cTempTrab)->POS0790 := '+' + StrTran(Transform((cTrab)->POS0790,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0811 := (cTrab)->POS0811
					(cTempTrab)->POS0814 := (cTrab)->POS0814
					(cTempTrab)->POS0819 := (cTrab)->POS0819
					(cTempTrab)->POS0823 := (cTrab)->POS0823
					(cTempTrab)->POS0833 := (cTrab)->POS0833
					(cTempTrab)->POS0838 := '+' + StrTran(Transform((cTrab)->POS0838,'@E '+ Replicate('9',15) + '.' + Replicate('9',4)),' ','0')
					(cTempTrab)->POS0859 := '+' + StrTran(Transform((cTrab)->POS0859,'@E '+ Replicate('9',15) + '.' + Replicate('9',4)),' ','0')
					(cTempTrab)->POS0880 := '+' + StrTran(Transform((cTrab)->POS0880,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0901 := '+' + StrTran(Transform((cTrab)->POS0901,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0922 := '+' + StrTran(Transform((cTrab)->POS0922,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0943 := '+' + StrTran(Transform((cTrab)->POS0943,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS0964 := '+' + Replicate('0',11) + ',' + Replicate('0',4)
					(cTempTrab)->POS0981 := '+' + Replicate('0',11) + ',' + Replicate('0',4)
					(cTempTrab)->POS0998 := '+' + Replicate('0',11) + ',' + Replicate('0',4)
					(cTempTrab)->POS1015 := '+' + Replicate('0',11) + ',' + Replicate('0',4)
					(cTempTrab)->POS1032 := '+' + Replicate('0',11) + ',' + Replicate('0',4)
					(cTempTrab)->POS1049 := (cTrab)->POS1049
					(cTempTrab)->POS1050 := (cTrab)->POS1050
					(cTempTrab)->POS1051 := SubStr((cTrab)->POS1051,7,7)
					(cTempTrab)->POS1063 := '+' + StrTran(Transform((cTrab)->POS1063,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS1084 := '+' + StrTran(Transform((cTrab)->POS1084,'@E '+ Replicate('9',11) + '.' + Replicate('9',4)),' ','0')
					(cTempTrab)->POS1101 := '+' + StrTran(Transform((cTrab)->POS1101,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS1122 := '+' + StrTran(Transform((cTrab)->POS1122,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS1143 := '+' + StrTran(Transform((cTrab)->POS1143,'@E '+ Replicate('9',11) + '.' + Replicate('9',4)),' ','0')
					(cTempTrab)->POS1160 := '+' + StrTran(Transform((cTrab)->POS1160,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS1181 := '+' + StrTran(Transform((cTrab)->POS1181,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS1202 := '+' + StrTran(Transform((cTrab)->POS1202,'@E '+ Replicate('9',11) + '.' + Replicate('9',4)),' ','0')
					(cTempTrab)->POS1219 := '+' + StrTran(Transform((cTrab)->POS1219,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS1240 := (cTrab)->POS1240
					(cTempTrab)->POS1241 := '+' + StrTran(Transform((cTrab)->POS1241,'@E '+ Replicate('9',11) + '.' + Replicate('9',4)),' ','0')
					
					// Codigo do municipio do emissor da Nota Fiscal POS1258
					
					// Nota Fiscal de Entrada
					If (cTrab)->ORIGEM == 'SD1'
						
						IF aScan(aUF,{|x| x[1] == (cTrab)->UF}) == 0
							
							(cTempTrab)->POS1258 := aUF[aScan(aUF,{|x| x[1] == "  "})][02] + (cTempTrab)->POS1258
							
						Else
							
							(cTempTrab)->POS1258 := aUF[aScan(aUF,{|x| x[1] == (cTrab)->UF})][02] +  (cTempTrab)->POS1258
							
						End IF
						
						// Nota Fiscal de Saida
					ElseIf (cTrab)->ORIGEM == 'SD2'
						
						(cTempTrab)->POS1258 := U_SYNCFIL(aSyncEnv[7],7,(cTrab)->FILIAL)
						
					End If
					
					(cTempTrab)->POS1265 := (cTrab)->POS1265
					(cTempTrab)->POS1269 := (cTrab)->POS1269
					(cTempTrab)->POS1270 := '+' + Replicate('0',17) + ',' + Replicate('0',2)
					(cTempTrab)->POS1291 := (cTrab)->POS1291
					(cTempTrab)->POS1293 := (cTrab)->POS1293
					(cTempTrab)->POS1295 := (cTrab)->POS1295
					(cTempTrab)->POS1297 := '+' + Replicate('0',17) + ',' + Replicate('0',2)
					(cTempTrab)->POS1318 := '+' + StrTran(Transform((cTrab)->POS1318,'@E '+ Replicate('9',03) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS1325 := '+' + StrTran(Transform((cTrab)->POS1325,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS1346 := '+' + StrTran(Transform((cTrab)->POS1346,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
					(cTempTrab)->POS1367 := '+' + Replicate('0',17) + ',' + Replicate('0',2)
					(cTempTrab)->POS1388 := (cTrab)->POS1388
					(cTempTrab)->POS1390 := (cTrab)->POS1390
					(cTempTrab)->POS1418 := '+' + StrTran(Transform((cTrab)->POS1418,'@E '+ Replicate('9',11) + '.' + Replicate('9',4)),' ','0')
					(cTempTrab)->POS1435 := (cTrab)->POS1435
					
					MsUnLock()
					
					nCount++
					
				End If
				
				(cTrab)->(DbSkip())
				
			End Do
			
			// --------------------------------------------------------------
			
			U_SYNCDAT(cTempTrab,aSyncEnv,nCount)
			
			(cTrab)->(DbCloseArea())
			(cTempTrab)->(DbCloseArea())
			fErase(Separa(cTempFile,'\',.T.)[3])
			
		End IF
		
	Else
		
		U_SYNCLOG(;
			'Agenda : ' +  SubStr(aSyncEnv[4],1,6) + CRLF +;
			'Tarefa : ' +  SubStr(aSyncEnv[4],7,6) + CRLF +;
			'Nao foi possivel montar o ambiente de geracao do layout ' + cLayout)
		
	End IF
	
	RpcClearEnv()
	
Return
